import Vue from 'vue';
import VueRouter from "vue-router";

import routes from './routes';


Vue.use(VueRouter)
//对编程式路由导航路由重复报错问题的解决
const originalPush= VueRouter.prototype.push
const originalReplace= VueRouter.prototype.replace

VueRouter.prototype.push=function(location,okCallback,errorCallback){
    if(okCallback===undefined&&errorCallback===undefined){
        originalPush.call(this,location).catch(()=>{});
    }else{
        originalPush.call(this,location,okCallback,errorCallback)
    }
}
VueRouter.prototype.replace=function(location,okCallback,errorCallback){
    if(okCallback===undefined&&errorCallback===undefined){
        originalReplace.call(this,location).catch(()=>{});
    }else{
        originalReplace.call(this,location,okCallback,errorCallback)
    }
}

const router =new VueRouter({
    mode:'history',
    routes,

});

router.beforeEach((to, from, next) => {
    if(to.path == '/login') {
      return next()
    }
    const token = window.sessionStorage.getItem('token')
    
    if(to.path == '/personal') {
      if(!token) {
        return next('/login')
      }
      return next()
    }
    next()
  })
  

export default router